<template>
  <div class="body">
    <div class="box">
      <el-form :model="user" :rules="rules" ref="registerRef">
        <div
          style="
            font-size: 20px;
            margin: 15px;
            font-weight: bold;
            text-align: center;
          "
        >
          注册
        </div>
        <el-form-item prop="name">
          <el-input placeholder="请输入昵称" v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input placeholder="请输入账号" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="user.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="confirmPass">
          <el-input
            type="password"
            placeholder="请再次输入密码"
            v-model="user.confirmPass"
          ></el-input>
        </el-form-item>
        <el-form-item prop="email">
          <el-input placeholder="请输入邮箱" v-model="user.email"></el-input>
        </el-form-item>
        <el-form-item prop="specialCode">
          <el-input
            placeholder="请输入202407"
            v-model="user.specialCode"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="login"
            >注册</el-button
          >
        </el-form-item>
        <div style="display: flex; height: 30px">
          <div style="flex: 1">
            <span
              style="color: #0f9876; cursor: pointer"
              @click="$router.push('/login')"
              >登入</span
            >
          </div>
          <div style="flex: 1; text-align: right">
            <span style="color: #0f9876; cursor: pointer" @click="forgetPassword">忘记密码</span>
          </div>
        </div>
      </el-form>
      <img src="@/assets/login-back.jpg" class="background-image" />
    </div>
  </div>
</template>

<script>
import { Lock } from "@element-plus/icons-vue";
import request from "@/utils/request";

export default {
  components: {
    Lock,
  },
  neme: "Register",
  data() {
    const validatePass2 = (rule, confirmPass, callback) => {
      if (confirmPass === "") {
        callback(new Error("请再次输入密码"));
      } else if (confirmPass !== this.user.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      user: {
        username: "",
        password: "",
        specialCode: "",
      },
      rules: {
        email: [
          { required: true, message: "邮箱不能为空", trigger: "blur" },
          {
            pattern: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/,
            message: "请输入正确的邮箱格式",
            trigger: "blur",
          },
        ],
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "账号长度不能超过 15 个字符",
            trigger: "blur",
          },
        ],
        name:  [
          { required: true, message: "昵称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 10,
            message: "昵称长度不能超过 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码长度应在 6 到 15 个字符之间",
            trigger: "blur",
          },
        ],
        confirmPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    forgetPassword() {
      window.alert("请联系管理员QQ1952287329");
    },
    login() {
      if (this.user.specialCode !== "202407") {
        window.alert("特殊码不正确");
        return;
      }
      this.$refs["registerRef"].validate((valid) => {
        if (valid) {
          // 验证通过
          request.post("/register", this.user).then((res) => {
            if (res.code === "200") {
              this.$router.push("/login");
            } else {
              window.alert(res.msg);
            }
          });
        }
      });
    },
  },
};
</script>

<style>
.body {
  height: 60vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: rgba(255, 255, 255, 0.289);
}
.box {
  display: flex;
  background-color: rgb(147, 211, 186);
  width: 300px;
  border-radius: 5px;
  overflow: hidden;
  justify-content: center;
  box-shadow: 0 4px 8px rgba(0.9, 0.9, 0.9, 0.2); /* 添加阴影 */
  position: relative; /* 设置为相对定位 */
}
.background-image {
  position: absolute; /* 设置为绝对定位 */
  width: 100%; /* 宽度覆盖整个父元素 */
  height: 100%; /* 高度覆盖整个父元素 */
  bottom: 0; /* 放置在底部 */
  left: 0; /* 放置在左边 */
  right: 0; /* 放置在右边 */
  top: 0; /* 放置在顶部 */
  z-index: -1; /* 背景图片的层级低于表单元素 */
  object-fit: cover; /* 使图片覆盖整个容器，不拉伸 */
}
</style>
